/**
 * Author:  LiShibin.
 * Date:    2017/7/5 0005.
 * File：   phone类.
 */
import BaseNative from './BaseNative'
import config from '../config'
import * as storage from '../storage'
import {Toast} from 'mint-ui'
class PhoneNative extends BaseNative {
  getType() {
    return 'PhoneNative'
  }

  toast(msg) {
    return plus.nativeUI.toast(msg) // eslint-disable-line
  }

  /***
   * 更新app
   * @param url app下载地址
   * @param downloadSuccess 下载成功的回调
   * @param downloadProgress 下载中的回调
   */
  updateApp(url, downloadSuccess, downloadProgress) {
    let downLTask = plus.downloader.createDownload(url, {}) // eslint-disable-line
    downLTask.start()
    downLTask.addEventListener('statechanged', function (task, status) {
      if (task.state === 4 && status === 200) { // 下载成功
        downloadSuccess(task)
        plus.runtime.install(task.filename, {}, function () { // eslint-disable-line
        }, function () {
          console.error('安装失败')
        })
      }
      if (task.state === 3) { // 下载中
        if (downloadProgress) {
          downloadProgress(task.downloadedSize, task.totalSize)
        }
      }
    })
  }

  /***
   * 微信登录
   * @return {Promise} resolve: obj 用户信息, reject: e, 错误信息
   */
  weixinLogin() {
    return new Promise((resolve, reject) => {
      // 获取登录认证通道
      plus.oauth.getServices(function (services) { // eslint-disable-line
        let wxServer = null
        for (let i = 0; i < services.length; i++) {
          if (services[i].id === 'weixin') {
            wxServer = services[i]
            break
          }
        }
        if (!wxServer) {
          console.error('获取微信登录认证通道失败')
          reject({msg: '获取微信登录认证通道失败'})
          return
        }
        // 登录认证
        wxServer.login(function () {
          wxServer.getUserInfo(function () {
            // 登录成功
            resolve(wxServer.userInfo)
          }, function (e) {
            console.error('获取用户信息失败', e)
            reject({msg: '获取用户信息失败'})
          })
        }, function (e) {
          console.error('登录认证失败', e)
          reject({msg: '登录认证失败'})
        })
      }, function (e) {
        console.error('获取登录认证通道失败', e)
        reject({msg: '获取登录认证通道失败'})
      })
    })
  }

  /***
   * 支付
   * @param type 0：微信 1：支付宝
   * @param order 支付订单对象
   * @return {Promise}
   */
  pay(type, order) {
    return new Promise((resolve, reject) => {
      let payType = {}
      switch (type) {
        case 0:
          payType = {id: 'wxpay', txt: '微信支付'}
          break
        case 1:
          payType = {id: 'alipay', txt: '支付宝'}
      }
      // 获取支付通道
      plus.payment.getChannels(function (channels) { // eslint-disable-line
        let payChanel = null
        for (var i in channels) {
          if (channels[i].id === payType.id) {
            payChanel = channels[i]
            break
          }
        }
        if (!payChanel) {
          console.error('获取' + payType.txt + '通道失败')
          reject({msg: '获取' + payType.txt + '通道失败'})
          return
        }
        if (!payChanel.serviceReady) {
          console.error('系统未安装' + payType.txt)
          reject({msg: '系统未安装' + payType.txt})
          return
        }
        plus.payment.request(payChanel, order, function (result) { // eslint-disable-line
          // 支付成功
          resolve(result)
        }, function (e) {
          console.error('支付失败', e)
          reject({msg: e.message})
        })
      }, function (e) {
        console.error('获取支付通道失败')
        reject({msg: '获取支付通道失败'})
      })
    })
  }

  /***
   * 微信支付
   * @param type 支付类型 0：微信支付，1：支付宝
   * @param param {paySn: ''}
   * @return {Promise}
   */
  // async weixinPay(param) {
  //   let order = await request.getWxPayOrder(param.paySn)
  //   return this.pay(0, order)
  // }
  weixinPay(param) {
    let paySn = param.paySn
    let key = storage.getKey()
    if (!key || key == '') {
      Toast('请先注册')
      return
    }
    let url = config.website + '/mobile/index.php?act=member_payment&op=pay&key=' + key + '&pay_sn=' + paySn + '&payment_code=wxpay'
    window.location.href = url
  }
}

export default PhoneNative